package me.numark.activities;

import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;

import com.kyleduo.switchbutton.SwitchButton;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import me.numark.R;
import me.numark.model.LogInResponse;
import me.numark.model.User;
import me.numark.net.NetworkConstants;
import me.numark.net.RequestServerTask;
import me.numark.utils.Global;
import me.numark.utils.HttpUtil;
import me.numark.utils.Settings;
import me.numark.utils.Utils;

public class PrivacyActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {

    @Bind(R.id.switch_hide_comments)
    SwitchButton btnHideComments;

    @Bind(R.id.switch_hide_likes)
    SwitchButton btnHideLikes;

    @Bind(R.id.decline_strange_messgae)
    SwitchButton btnDeclineMsg;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_privacy;
        super.onCreate(savedInstanceState);
        initUi();
    }

    void initUi(){
        titleTextView.setText(R.string.setting_privacy);
        btnHideComments.setChecked(Settings.getSwitchTopicSecret());
        btnHideLikes.setChecked(Settings.getSwitchLikeSecret());
       // btnDeclineMsg.setChecked(Settings.getSwitchDialog());
        btnDeclineMsg.setOnCheckedChangeListener(this);
        btnHideComments.setOnCheckedChangeListener(this);
        btnHideLikes.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.switch_hide_comments:
            case R.id.switch_hide_likes:
            case R.id.decline_strange_messgae:
                setMessageReminder();
                break;
        }

    }

    private void setMessageReminder() {
        new RequestServerTask<LogInResponse>(LogInResponse.class) {
            @Override
            protected String requestServer() {
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("topic_secret", Boolean.toString(btnHideComments.isChecked()));
                //mUser.is_topic = switchComments.isChecked();
                map.put("like_secret", Boolean.toString(btnHideLikes.isChecked()));
                //mUser.is_like = switchLikes.isChecked();
                map.put("is_dialog",Boolean.toString(btnDeclineMsg.isChecked()));
                String url = String.format(NetworkConstants.MESSAGE_SECRET_URL, Global.getCurrentUid());
                return HttpUtil.post(url, map);
            }

            @Override
            protected void onSuccess(LogInResponse result) {
                Settings.setSwitchTopicSecret(result.data.topic_secret);
                Settings.setSwitchLikeSecret(result.data.like_secret);
               // Settings.setSwitchDialog(result.data.is_dialog);
                Utils.showSnackbar(rootView,R.string.commit_succ);
            }
        }.start();
    }
}
